<?php

class DatacatAction extends Action 
{
	public function _initialize()
	{
		$this->assign('jumpUrl',__URL__);
 		if(!isset($_SESSION['adminid']))
 		{ 
 			 $url=__APP__.'/Index/login';
 			 redirect($url);return;
 		}		
	}   
    public function index()
    {
		$Datacat=D("Datacat");
		$count=$Datacat->count();
		import("ORG.Util.Page");
		$listRows=10;
		$p=new page($count,$listRows);
		$list=$Datacat->findAll(null,'*','orderid desc,addtime desc',$p->firstRow.','.$p->listRows);
		$page=$p->show();
		if ($list!==false) 
		{					
			$this->assign('page',$page);
			$this->assign('list',$list);
		}
		$this->display();
    }
    public function add()
    {
    	if(!$this->isPost())
    	{
    		$this->display();
    	}
    	else
    	{
    		$name=isset($_POST['name'])?trim($_POST['name']):'';
    		$orderid=isset($_POST['orderid'])?(int)($_POST['orderid']):0;
    		
    		$Datacat=D("Datacat");
            if($orderid==0)
            {	
            	$orderid=$Datacat->max('orderid');
            	$orderid=$orderid+1;
            }    
            $data=array(
            	'name'=>$name,
            	'orderid'=>$orderid,
            );
    		if(!$Datacat->create($data)){
    			$this->error($Datacat->getError());return;
    		}
    		if(!$Datacat->add()){ 		
    			$this->error(L('_INSERT_FAIL_'));return;
    		}
			$this->cache();	    		
    		$this->assign('jumpUrl',__URL__);
    		$msg="技术资料分类添加成功";
    		$this->success($msg);
    	}
    }
    public function edit()
    {
    	if(!$this->isPost())
    	{
			$id=isset($_GET['id'])?(int)($_GET['id']):0;
			if (!$id){
				$this->error(L('_SELECT_NOT_EXIST_'));return;
			}			
			$temp=D('Datacat')->find($id);
			if (!$temp){
					$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			$this->assign('vo',$temp);
			$this->display();			    			
    	}
    	else
    	{
			$id=isset($_POST['id'])?(int)($_POST['id']):0;
			$temp=D('Datacat')->find($id);
			if ($id==0 || !$temp){ 
				$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			$Datacat=D("Datacat");
			if(!$Datacat->create()){
				$this->error($Datacat->getError());return;
			}
			if(!$Datacat->save()){
				$this->error(L('_UPDATE_FAIL_'));return;
			}
			$this->cache();			
			$this->assign('jumpUrl',__URL__);
			$msg="技术资料分类修改成功";
			$this->success($msg);			
    	}    	
    }
	public function del()
	{
		$id=isset($_GET['id'])?(int)($_GET['id']):0;
		if (!$id){ $this->error(L('_SELECT_NOT_EXIST_'));return;}
		$Datacat=D("Datacat");
		$temp=$Datacat->find($id);
		if (!$temp){ $this->error(L('_SELECT_NOT_EXIST_'));return;}
		if(!$Datacat->deleteById($id))
		{
			$this->error(L('_DELETE_FAIL_'));return;
		}
		$this->cache();
		$this->redirect();
	}
	private function cache()
	{
		$category=D("Datacat");
		$list=$category->findAll(null,'id,name','orderid desc,addtime desc');
		$temp=array();
		if($list)
		{
			foreach($list as $row)
			{ 
				$key=$row['id']; $value=$row['name']; $temp[$key]=$value;
			}			
		}
		$filename=C('MYCACHE_PATH').'/datacat.php';
		if(@file_exists($filename)){ @unlink($filename);}
    	$content="<?php\n \$datacat=".var_export($temp,true)."\n?>";
    	@!file_put_contents($filename,$content);	
	}		
}
?>